/*
 * @Author: szx
 * @Date: 2022-04-12 13:19:35
 * @LastEditTime: 2022-04-12 13:26:28
 * @Description:
 * @FilePath: \leetcode\剑指offerII\052\052.js
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var increasingBST = function (root) {
    let prev = null;
    let res = null;
    const dfs = (root) => {
        root.left && dfs(root.left);
        if (!prev) {
            prev = root;
            res = root;
        } else {
            root.left = null;
            prev.right = root;
            prev = root;
        }
        root.right && dfs(root.right);
    };
    dfs(root);
    return res;
};
